package com.bawei.play.vm

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.bawei.common.vm.BaseViewModel
import com.bawei.play.intent.PlayViewIntent
import com.bawei.play.model.PlayRepository
import com.bawei.play.state.PlayUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * Author: Jason Zhang
 * Date: 2025/1/16
 * Desc: 视频播放ViewModel
 */

@HiltViewModel
class PlayViewModel @Inject constructor(private val repository: PlayRepository): BaseViewModel<PlayViewIntent>(){

    /**
     * SharedFlow与StateFlow的区别
     * 1.SharedFlow有缓存区，可以缓存状态数据，StateFlow无缓存区
     * 2.SharedFlow可以发送相同状态，StateFlow不可以，只能为单一状态数据
     * 3.SharedFlow可以处理多个事件，适用于多个订阅方同时订阅，StateFlow不可以
     */

    private val _uiState = MutableSharedFlow<PlayUiState>()
    val uiState: SharedFlow<PlayUiState> = _uiState.asSharedFlow()

    override fun dispatchIntent(intent: PlayViewIntent) {
        when(intent){
            is PlayViewIntent.LoadSimpleVideoListById -> loadSimpleViewById(intent.channelId, intent.page, intent.pageSize)
            is PlayViewIntent.LoadRecommendSimpleVideoList -> loadRecommendSimpleVideo(intent.page, intent.pageSize)
        }
    }


    private fun loadRecommendSimpleVideo(page: Int, pageSize: Int){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.getRecommendVideo(page, pageSize).collect{ list ->
                    if (list.isNotEmpty()){
                        Log.i("======", "loadRecommendSimpleVideo=====>>>>: ${list?.size}")
                        _uiState.emit(PlayUiState.LoadSimpleVideoSuccess(list))
                    } else{
                        _uiState.emit(PlayUiState.LoadSimpleVideoFail("没有推荐视频"))
                    }
                }
            } catch (e:Exception){
                e.printStackTrace()
                _uiState.emit(PlayUiState.LoadSimpleVideoFail("请求失败！！"))
            } finally {
                cancel()
            }
        }

    }

    private fun loadSimpleViewById(channelId: String, page:Int, pageSize: Int){
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.getSimpleVideoByChannelId(channelId, page, pageSize).collect{ list ->
                    if (list.isNotEmpty()){
                        _uiState.emit(PlayUiState.LoadSimpleVideoSuccess(list))
                    } else{
                        _uiState.emit(PlayUiState.LoadSimpleVideoFail("没有此分类视频"))
                    }
                }
            } catch (e:Exception){
                e.printStackTrace()
                _uiState.emit(PlayUiState.LoadSimpleVideoFail("请求失败！！"))
            } finally {
                cancel()
            }
        }



    }


}